<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/6/11
  Time: 14:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">一级菜单</label>
        <div class="layui-input-inline">
            <select id="one" lay-filter="one"></select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">二级菜单</label>
        <div class="layui-input-inline">
            <select id="two"></select>
        </div>
    </div>
</form>
<script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script>
    layui.use(['layer','form','jquery'],function () {
        var layer=layui.layer
            , form=layui.form;
        var $=layui.$;
        layer.msg("Hello word");
        //发异步请求获取一级菜单
        $.get("${pageContext.request.contextPath}/category/childCategory",{parentid:-1},function (result) {
            if (result.code==200){
                var html="<option value=''>请选择</option>";
                for (var i=0;i<result.data.length;i++){
                    html+="<option value='"+result.data[i].id+"'>"+result.data[i].name+"</option>"
                }
                $("#one").html(html);
                form.render();//更新全部
            }
        },"json")
        //lect 框监听事件
        form.on('select(one)', function(data){
            if (data.value==''){//如果是请选择，则将二级菜单，清空
                $("#two").empty();
                form.render();
                return;
            }
            $.get("${pageContext.request.contextPath}/category/childCategory",{parentid:data.value},function (result) {
                if (result.code==200){
                    var html="<option value=''>请选择</option>";
                    for (var i=0;i<result.data.length;i++){
                        html+="<option value='"+result.data[i].id+"'>"+result.data[i].name+"</option>"
                    }
                    $("#two").html(html);
                    form.render();//更新全部
                }
            },"json")
        });

    });
</script>
</body>
</html>
